package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoleBean;
import com.project.mapper.IPowerMapper;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper roleMapper;
    @Autowired
    private IPowerMapper powerMapper;
    @Override
    public List<RoleBean> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<RoleBean> findByItem(Integer pageNO) {

        return roleMapper.selectPage(new Page(pageNO,3),null);
    }

    @Override
    public void add(RoleBean role, Integer[] powerIdArray) {
        roleMapper.insert(role);
        //获取角色主键之后再进行角色/权限中间表的添加
        roleMapper.addCenter(role.getId(), powerIdArray);
    }

    @Override
    public void del(Integer id) {
        roleMapper.delPower(id);

        roleMapper.delUser(id);

        roleMapper.deleteById(id);
    }

    @Override
    public RoleBean findById(Integer id) {
        //通过id查询角色信息
        RoleBean role = roleMapper.selectById(id);
        //同时查询该角色对应的权限集合
        role.setPowerSet(powerMapper.findByRole(id));
        return role;
    }

    @Override
    public void update(Integer id, Integer[] powerIdArray) {
        //先删除角色/权限中间表的信息
        roleMapper.delPower(id);
        //然后再添加该角色的新权限
        roleMapper.addCenter(id,powerIdArray);
    }
}
